﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _TListBox {
    public partial class TListItemGroup : Control {
        StringFormat sf = new StringFormat();
        Pen splitLinePen;
        public TListBox tListBox;
        //Color groupTextColor = Color.Blue;
        List<TListItem> itemList = new List<TListItem>();
        string gong = "共";
        string xiang = "项";


        public event Action<TListItemGroup, int> OnClicked;

        bool expand = false;
        public bool Expand {
            get { return expand; }
            set {
                expand = value;
                if (expand) {
                    Height = itemList.Count * tListBox.ItemHeigth;
                }
                else {
                    Height = tListBox.ItemHeigth;
                }
                this.Invalidate();
            }
        }

        public TListItemGroup() {
            SetStyle(ControlStyles.ContainerControl
                | ControlStyles.CacheText
                | ControlStyles.OptimizedDoubleBuffer
                | ControlStyles.AllPaintingInWmPaint
                | ControlStyles.ResizeRedraw
                | ControlStyles.Selectable, true);
            InitializeComponent();
            sf.Alignment = StringAlignment.Near;
            sf.LineAlignment = StringAlignment.Center;
        }

        private Color groupTextColor = Color.Blue;
        private SolidBrush groupBrush = new SolidBrush(Color.Blue);

        public Color GroupTextColor {
            get { return groupTextColor; }
            set {
                groupTextColor = value;
                groupBrush = new SolidBrush(groupTextColor);
                Invalidate();
            }
        }

        public void AddItem(Image icon, string text, object tag = null) {
            itemList.Add(new TListItem { icon = icon, text = text, tag = tag });
        }

        public TListItem GetItem(int index) {
            return itemList[index];
        }

        protected override void OnPaint(PaintEventArgs pe) {
            Graphics g = pe.Graphics;
            g.Clear(tListBox.BackColor);
            if (splitLinePen == null)
                splitLinePen = new Pen(tListBox.LineColor, 1f);
            // 标题
            DrawTitle(g);
            // 子项目
            if (Expand) {
                int y = tListBox.ItemHeigth;
                for (int i = 1; i < itemList.Count; i++) {
                    TListItem listItem = itemList[i];
                    listItem.bounds.Y = y;
                    g.DrawImage(listItem.icon, new Rectangle(20, y + 4, 20, 20));
                    g.DrawString(listItem.text, tListBox.Font,
                        Brushes.Black, new Rectangle(40 + 2, y, Width, tListBox.ItemHeigth), sf);
                    y += tListBox.ItemHeigth;
                    // 每一行的分割线
                    g.DrawLine(splitLinePen, 0, y-1, Width, y-1);

                }
            }
        }

        void DrawTitle(Graphics g) {
            TListItem titleItem = itemList[0];
            int fontSize = (int)tListBox.Font.Size;
            // 图标
            g.DrawImage(titleItem.icon, new Rectangle(10, 6, 15, 15));
            // 标题
            g.DrawString(titleItem.text, tListBox.Font, groupBrush
                , new Rectangle(31, 0, Width, tListBox.ItemHeigth), sf);
            // 线
            Point p1 = new Point(31 + titleItem.text.Length * fontSize + 15, 14);
            Point p2 = new Point(p1.X + 25, p1.Y);
            g.DrawLine(Pens.Black, p1, p2);
            // 汇总
            Rectangle rect = new Rectangle(p2.X + fontSize-5, 0, Width, tListBox.ItemHeigth);
            g.DrawString(gong, tListBox.Font, Brushes.Black, rect, sf);

            string itemCount = (itemList.Count - 1).ToString();
            rect.X += (fontSize + 6);
            g.DrawString(itemCount, tListBox.Font, groupBrush, rect, sf);

            rect.X = rect.X + itemCount.Length * fontSize + 1;
            g.DrawString(xiang, tListBox.Font, Brushes.Black, rect, sf);
            // 折叠状态图片
            Image stateImg = expand ? tListBox.ExpandImage : tListBox.UnExpandImage;
            g.DrawImage(stateImg, new Rectangle(Width - 50, 7, 15, 15));
            // 每一行的分割线
            g.DrawLine(splitLinePen, 0, tListBox.ItemHeigth - 1, Width, tListBox.ItemHeigth - 1);
        }

        protected override void OnMouseDown(MouseEventArgs e) {
            base.OnMouseDown(e);
            //Console.WriteLine(e.Location);
            int row = e.Location.Y / tListBox.ItemHeigth;
            Console.WriteLine(e.Location + "," + row);
            if (row == 0) {
                Expand = !Expand;
                if (tListBox.Current != this) {
                    if (tListBox.Current != null && tListBox.Current.Expand) {
                        tListBox.Current.Expand = false;
                    }
                    tListBox.Current = this;
                }


                //if (Clicked != null) {
                //    Clicked(this, 0);
                //}
            }
        }

        protected override void OnMouseClick(MouseEventArgs e) {
            base.OnMouseClick(e);
            int row = e.Location.Y / tListBox.ItemHeigth;
            //Console.WriteLine(e.Location + "," + row);
            if (OnClicked != null)
                OnClicked(this, row);
        }

        //void DrawTitle(Graphics g)
        //{
        //    TListItem titleItem = itemList[0];
        //    g.DrawImage(titleItem.icon, new Rectangle(10, 4, 22, 22));
        //    g.DrawString(titleItem.text, tListBox.Font,
        //     Brushes.Black, new Rectangle(38, 0, Width, tListBox.ItemHeigth), sf);

        //    SizeF textSize = g.MeasureString(titleItem.text, tListBox.Font);
        //    Point p1 = new Point(38 + (int)textSize.Width + 10, 15);
        //    Point p2 = new Point(p1.X + 10, p1.Y);
        //    g.DrawLine(Pens.Black, p1, p2);

        //    g.DrawString(gong, tListBox.Font,
        //     Brushes.Black, new Rectangle(p2.X + 2, 0, Width, tListBox.ItemHeigth), sf);
        //    textSize = g.MeasureString(gong, tListBox.Font);

        //    string itemCount = (itemList.Count - 1).ToString();
        //    Rectangle rect = new Rectangle(p2.X + 2 + (int)textSize.Width + 2, 0, Width, tListBox.ItemHeigth);
        //    g.DrawString(itemCount, tListBox.Font, groupBrush, rect, sf);
        //    textSize = g.MeasureString(itemCount, tListBox.Font);

        //    rect.X = rect.X + (int)textSize.Width + 2;

        //    g.DrawString(xiang, tListBox.Font, Brushes.Black, rect, sf);
        //}
    }

    public class TListItem {
        public Image icon;
        public string text;

        public object tag;

        public Rectangle bounds;


    }
}

